/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Interface2.java
 *
 * Created on 24/11/2010, 21:31:36
 */

package Interface;

import Dados.ServidorModel;
import com.sun.java.swing.plaf.windows.WindowsLookAndFeel;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Toolkit;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import railbee.banco.dao.ServidoresDao;
import railbee.banco.dto.Servidores;
import railbee.banco.exceptions.ServidoresDaoException;
import railbee.banco.factory.ServidoresDaoFactory;
import railbee.banco.jdbc.ResourceManager;

/**
 *
 * @author jerry
 */
public class Interface2 extends javax.swing.JFrame {
    private JanelaConectaServidores janelaServ;
    static int conectado = 0;
    private Connection connection;
//GridBagConstraints const;
    /** Creates new form Interface2 */
    public Interface2() throws SQLException {
        initComponents();
         this.setIconImage(Toolkit.getDefaultToolkit().getImage("src/Imagens/logo_railbee_p3.png"));
        //jTable1.setVisible(false);
       //jPanel1.setVisible(false);
       jLayeredPane1.setLayer(jPanel1, 0);
       jLayeredPane1.setLayer(jScrollPane1, 1);
       connection = ResourceManager.getConnection();
         //GridBagLayout layout = new GridBagLayout();
        // GridBagLayout layout2 = new GridBagLayout();
         //GridBagConstraints c = new GridBagConstraints();
        // c.weightx = 1;
        // c.weighty = 1;
        // c.gridwidth = 1;
        // c.gridheight = 1;
        // c.fill = GridBagConstraints.BOTH;
        // layout.setConstraints(jPanel1, c);
        // layout.setConstraints(jPanel2, c);
         //layout2.setConstraints(jScrollPane1, c);
       //jLayeredPane1.setLayout(layout);
        jScrollPane1.setVisible(false);
        this.setLocationRelativeTo(null);
        jDialog1.setLocationRelativeTo(this);

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        tabelaServidores = new javax.swing.JTable();
        jButton4 = new javax.swing.JButton();
        jToolBar1 = new javax.swing.JToolBar();
        btConect = new javax.swing.JButton();
        btTrens = new javax.swing.JButton();
        btUsuarios = new javax.swing.JButton();
        jLayeredPane1 = new javax.swing.JLayeredPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();

        jDialog1.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jDialog1.setTitle("teste");
        jDialog1.setAlwaysOnTop(true);
        jDialog1.setModal(true);

        jButton1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton1.setText("Conectar-se a todos os servidores");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        tabelaServidores.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Estação", "IP", "Porta"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Integer.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        tabelaServidores.setRowHeight(20);
        jScrollPane2.setViewportView(tabelaServidores);

        jButton4.setFont(new java.awt.Font("Tahoma", 1, 12));
        jButton4.setText("Conectar-se apenas aos selecionados");

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton4, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE))
                .addContainerGap())
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jDialog1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 228, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 34, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 36, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RailBee");
        setAlwaysOnTop(true);
        setLocationByPlatform(true);
        setMinimumSize(new java.awt.Dimension(800, 600));

        jToolBar1.setRollover(true);
        jToolBar1.setPreferredSize(new java.awt.Dimension(100, 25));

        btConect.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/C_conectado.png"))); // NOI18N
        btConect.setFocusable(false);
        btConect.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btConect.setMaximumSize(new java.awt.Dimension(57, 75));
        btConect.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btConect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConectActionPerformed(evt);
            }
        });
        jToolBar1.add(btConect);

        btTrens.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/trem-50.png"))); // NOI18N
        btTrens.setFocusable(false);
        btTrens.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btTrens.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btTrens.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btTrensActionPerformed(evt);
            }
        });
        jToolBar1.add(btTrens);

        btUsuarios.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/Login Manager.png"))); // NOI18N
        btUsuarios.setFocusable(false);
        btUsuarios.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btUsuarios.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btUsuarios.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btUsuariosActionPerformed(evt);
            }
        });
        jToolBar1.add(btUsuarios);

        jLayeredPane1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLayeredPane1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jLayeredPane1.setEnabled(false);
        jLayeredPane1.setRequestFocusEnabled(false);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setFocusable(false);
        jPanel1.setMinimumSize(new java.awt.Dimension(800, 300));
        jPanel1.setPreferredSize(new java.awt.Dimension(800, 517));

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/logo_railbee.png"))); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(250, 250, 250)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 800, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 500, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel1.setBounds(1, 2, 800, 500);
        jLayeredPane1.add(jPanel1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jScrollPane1.setBorder(null);
        jScrollPane1.setEnabled(false);
        jScrollPane1.setRequestFocusEnabled(false);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "Trem", "Estação", "Velocidade", "Pressão", "Tensão", "Corrente", "Passageiros", "Posição"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true, false, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jTable1.setRowHeight(25);
        jScrollPane1.setViewportView(jTable1);

        jScrollPane1.setBounds(1, 1, 800, 500);
        jLayeredPane1.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        jMenu1.setText("File");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");
        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 575, Short.MAX_VALUE)
                .addGap(225, 225, 225))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLayeredPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 317, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btConectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConectActionPerformed
        try {
            // TODO add your handling code here:
            ServidoresDao servidoresDao = ServidoresDaoFactory.create(connection);
            Servidores[] servidores = servidoresDao.findAll();
            ArrayList<String[]> listaServ = new ArrayList<String[]>();
           for(Servidores s: servidores){

               listaServ.add(new String[]{s.getSiglaEstacao(),s.getIpServidor(), Integer.toString(s.getPortaServidor())});
           }
            ServidorModel servidorModel = new ServidorModel(listaServ);
            for(String[] serv: listaServ){
                servidorModel.add(serv);
            }
            //tabelaServidores.isEditing();
            tabelaServidores.setModel(servidorModel);
            //servidorModel.fireTableDataChanged();

            jDialog1.pack();
            jDialog1.setVisible(true);
        } catch (ServidoresDaoException ex) {
            Logger.getLogger(Interface2.class.getName()).log(Level.SEVERE, null, ex);
        }
     
        
    }//GEN-LAST:event_btConectActionPerformed

    private void btTrensActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btTrensActionPerformed
        // TODO add your handling code here:
        // jLayeredPane1.setLayer(jPanel1, 1);
        //jLayeredPane1.setLayer(jPanel1, 1);
       //  jScrollPane1.setVisible(false);

       
         //janela.;
         janela  = new JanelaTrens();
         janela.setVisible(true);
         janela.setAlwaysOnTop(rootPaneCheckingEnabled);


    }//GEN-LAST:event_btTrensActionPerformed

    private void btUsuariosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btUsuariosActionPerformed
        // TODO add your handling code here:
        jScrollPane1.setVisible(true);
     jLayeredPane1.setLayer(jScrollPane1, 1);
       
    }//GEN-LAST:event_btUsuariosActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        conectado = 1;
           jScrollPane1.setVisible(true);
            jLayeredPane1.setLayer(jScrollPane1, 1);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(new WindowsLookAndFeel());
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    new Interface2().setVisible(true);
                } catch (SQLException ex) {
                    Logger.getLogger(Interface2.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

   JanelaTrens janela;
// public int conectado = 0;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btConect;
    private javax.swing.JButton btTrens;
    private javax.swing.JButton btUsuarios;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton4;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLayeredPane jLayeredPane1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTable tabelaServidores;
    // End of variables declaration//GEN-END:variables



}
